package com.yj.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    @Autowired
    public void initRabbitMQ(){

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost("192.168.25.200");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);

        Exchange orderExchange = new DirectExchange("orderExchange");
        rabbitAdmin.declareExchange(orderExchange);

        Queue orderQueue = new Queue("orderQueue");
        rabbitAdmin.declareQueue(orderQueue);

        Binding binding = new Binding("orderQueue",
                Binding.DestinationType.QUEUE,
                "orderExhange",
                "order",
                null);

        rabbitAdmin.declareBinding(binding);



    }
}
